Lecture 04

Statistics 422

R Shiny

  • R Shiny is used for creaing interactive web applications in R.

  • It isn’t really for us, it’s for others to interactively explore our data.

  • We can also use R Shiny to analyze data and create graphics and summaries - but the point is it is to be shared with others on the internet.

  • As the designers of an R Shiny app, we design it to allow other uses to select predefined options or filters.

Example

  • I have access to earthquake data in Southern California

  • I can create a Shiny app so my users could select a specific time frame and map the earthquakes.

  • They might adjust their choices to see data from different years, or click on the map to get different information about a particular are.

Shiny

  • Your users can change what they see by interacting with the web application directly. This could be through various methods

  • If your data updates, Shiny can automatically update the data. This means users always see the most current information.

  • You can design your Shiny app to look and behave exactly how you want.

How does it work?

  • We use R to write the code for what the app should do with the data, e.g. compute summary stats, draw graphs.
  • We also use R to design what the app will look like—where the buttons, graphs, and sliders will be.
  • R Shiny turns converts your code into a web app that can be accessed through a browser

Creation

In RStudio

Select File-New File-Shiny Web App

Set Up - choices

Choose a name, choose a single file (app.R when just starting out), choosea location on your local drive

In RStudio

There should now be a new folder with an RStudio created demo file in it. Click on “Run App”

In RStudio

If you run the app (green triangle icon on the upper right) the default Old Faithful Shiny app should pop up

In RStudio

You can open it in your browser to get a better look

In RStudio

  • I have an example file that does nothing. It is named Shiny0_proof.R
  • It exists to give you the absolute minimum that is needed to make Shiny run

In RStudio

  • The blank window is correct, now we will expand on it.

Commands

Websites

Cheatsheet for Shiny

  • In “Building an App” it lays out the structure for you.

UI

UI Input Choices

UI Closeup (shiny1_ui_only.R)

  • titlePanel() and sliderInput() notice the comma just before h1()

UI Closeup (shiny1_ui_only.R) (cont’d)

  • various text control

UI Closeup Result

  • Rendered Result

UI Closeup with image and bullets

UI Closeup with image and bullets result

The UI is an HTML document

## ### Remainder of shiny1_ui_only.R

At this point

  • There is no data, moving the slider just moves the slider
  • There is user input being collected, but the input isn’t being processed
  • The ui ends with the close parenthesis (note no comma)
  • We have not given instructions to the server for output
  • shinyApp(ui = ui, server = server) runs and we have a Shiny app

Server

Build Server side with render & output

  • See shiny2-ui_server_output.R
  • The remainder of Shiny is rendering input then results are output.

UI modified

  • In shiny2-ui_server_output.R, user input will be passed to the server and rendered.
  • UI changes a bit with some output functions - plotOutput(), textOutput(), DTOutput() and tableOutput()
  • The output functions will be connected to render functions

A closer look

  • There are four render-output pairs,
  • the rendering occurs in server
  • the first of the render functions here has a variable passed to it from ui:

Notes on user input and render

  • Within the {} in renderPlot() is the R code that will build a histogram
  • Only R code in the server portion, notice, just hist() from base R no html etc.
  • Access the input values with input$
  • The “num” in input$num is the value assigned to inputID in the sliderInput of the UI

render-output notes

  • server is a function then built using render functions,
  • the output contains values which are passed back to UI
  • Some values could originate from the server side

Result

Result

Ready to Publish?

  • Not that I would publish this, but I could.
  • Publishing options, see publishing Shiny
  • You can also have users download your app.R
  • ex. runGist("8ad7b367f0d4d9947a94c5387900b2bd") make sure your Shiny app has a standard name like app.R and not my non-standard ones.